/*
author: pace_zhong@foxmail.com
date: 2016-03-10
devDependencies: jQuery
*/
define(["constant"], function (Constant) {
    var send = function (options) {
        var $btn = options.antiDuplicationBtn;// 点击按钮，做一些loading样式
        var $container = options.container;// 容器，做一些loading和错误展示
        if ($btn) {
            $btn.blur();
        }

        options.crossDomain = true;
        options.xhrFields = {
            withCredentials: true
        };

        options.url = (options.isAbsoluteUrl === false ? "" : Constant.param.domain) + options.url;
        if (!options.data) options.data = {};
        if (options.isTakeToken !== false) options.data.pin = $.cookie(Constant.param.ckGuidName);
        //options.data.imei = Constant.param.imei;

        /*options.beforeSend = function(){
           $("#loading").show();
        }*/

        options.success = function (data) {
            if (data == 'noLogin') {
                location.href = document.getElementsByTagName("base")[0].getAttribute("href") + "login/login.html"
            }
            if (data == 'noRole') {
                Constant.fn.Message('F', data.msg);
                return;
            }
            Constant.fn.btnStatusManage.enable($btn);
            options.callback(data);
        };


        options.error = function () {
            Constant.fn.btnStatusManage.enable($btn);
            if (typeof options.callError === "function") {
                options.callback();
            }
        };

        var newOptions = $.extend(true, {type: "POST", timeout: Constant.param.timeout}, options);

        Constant.fn.btnStatusManage.disabled($btn);
        $.ajax(newOptions);
    };

    //打开一个新的连接
    var open = function (options) {
        var $btn = options.antiDuplicationBtn;
        if ($btn) {
            $btn.blur();
        }

        options.url = (options.isAbsoluteUrl === false ? "" : Constant.param.domain) + options.url;
        if (!options.data) options.data = {};
        if (options.isTakeToken !== false) options.data.token = $.cookie(Constant.param.ckGuidName);
        //options.data.imei = Constant.param.imei;

        var param = "";
        for (k in options.data) {
            param += k + "=" + options.data[k] + "&";
        }
        ;

        if (options.url.indexOf("?") > 0) {
            window.open(options.url + param);
        } else {
            window.open(options.url + "?" + param);
        }
    };

    // 上传文件
    var filesUpload = function (options) {
        var $btn = options.antiDuplicationBtn;
        var $container = options.container;
        if ($btn) $btn.blur();

        options.url = (options.isAbsoluteUrl === false ? "" : Constant.param.domain) + options.url;

        if (!options.data) options.data = {};

        if (options.isTakeToken !== false) options.data.token = $.cookie(Constant.param.ckGuidName);

        var formData = new FormData();
        $.each(options.data, function (name, value) {
            formData.append(name, value);
        });

        options.data = formData;

        options.success = function (data) {
            Constant.fn.btnStatusManage.enable($btn);
            // Constant.fn.containerStatusManage.complete($container);
            if (data.code == 0 && typeof options.callSuccess === "function") {
                options.callSuccess(data.obj);
            } else if (data.code !== 0) {
                if (typeof options.callErrorWithCode === 'function') {
                    options.callErrorWithCode(data);
                }
            }
        };

        options.error = function () {
            Constant.fn.btnStatusManage.enable($btn);
            if (typeof options.callError === "function") {
                options.callError();
            }
        };

        var newOptions = $.extend(true, {
            type: "POST",
            timeout: Constant.param.timeout,
            cache: false,
            contentType: false,
            processData: false
        }, options);

        Constant.fn.btnStatusManage.disabled($btn);

        $.ajax(newOptions);
    };
    return {
        send: send,
        open: open,
        filesUpload: filesUpload
    }
});
